<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Namustar {
	
	/**
	 * Function that accepts two strings that are datetimes and returns a string that describes the difference in time.
	 * The first parameter is the first datetime. If there are no second parameter, it is regarded as present datetime.
	 *
	 * @param string $first_time First datetime.
	 * @param optional string $last_time Second datetime. Default is present datetime.
	 * @return string
	 */
	public function time_difference_string($first_time, $last_time = 0)
	{
		//Getting the UNIX-timestamps.
		$first_stamp = strtotime($first_time);
		$last_stamp = strtotime($last_time);
		
		//If there are no second datetime the present one is used instead.
		if ($last_stamp === FALSE)
			$last_stamp = time();
		
		//Calculate difference in seconds.
		$seconds = $last_stamp - $first_stamp;
		
		//Calculate the difference in weeks, days, minutes and seconds.
			$minutes = intval($seconds / 60);
			$seconds = $seconds - ($minutes * 60);
			
			$hours = intval($minutes / 60);
			$minutes = $minutes - ($hours * 60);
			
			$days = intval($hours / 24);
			$hours = $hours - ($days * 24);
			
			$weeks = intval($days / 7);
			$days = $days - ($weeks * 7);
		
		//Return a string that describes the difference in time.
		return "$weeks weeks, $days days, $hours hours, $minutes minutes and $seconds seconds";
	}
	
	/**
	 * Function that returnes a help image in HTML-format with the help text as title atribute.
	 *
	 * @param string $title_text The help text that are to be shown when hovering above the picture.
	 * @param array $parameters Array with extra attribute for customisation of the help image.
	 * @return string The help image in HTML format.
	 */
	public function help_image($title_text, $parameters = array())
	{
		//Setting class.
		if (!isset($parameters['class'])) {
			$parameters['class'] = 'noborder';
		}
		
		//Setting image width.
		if (!isset($parameters['width'])) {
			$parameters['width'] = 12;
		}
		
		//Setting image height.
		if (!isset($parameters['height'])) {
			$parameters['height'] = 12;
		}
		
		//Returning HTML-string for the help image.
		return '<img src="' . base_url() . 'images/help.png" class="' . $parameters['class'] . '" title="' . $title_text . '" width="' . $parameters['width'] . '" height="' . $parameters['height'] . '" />';
    }
}

/* End of file Time.php */